package com.hsjk.szwj.module.pay.util;

import cn.hutool.extra.spring.SpringUtil;
import com.hsjk.szwj.framework.common.exception.ServiceException;
import com.hsjk.szwj.framework.common.util.spring.SpringBeansUtil;
import com.hsjk.szwj.module.pay.dal.dataobject.config.MchAppConfigContext;
import com.hsjk.szwj.module.pay.dal.dataobject.config.PlspayConfig;
import com.hsjk.szwj.module.pay.dal.dataobject.config.PlspayNormalMchParams;
import com.hsjk.szwj.module.pay.dal.dataobject.order.PayOrderDO;
import com.hsjk.szwj.module.pay.enums.IfCodeEnum;
import com.hsjk.szwj.module.pay.service.ConfigContextQueryService;
import com.jeequan.jeepay.Jeepay;
import com.jeequan.jeepay.JeepayClient;
import com.jeequan.jeepay.exception.JeepayException;
import com.jeequan.jeepay.model.PayOrderCreateReqModel;
import com.jeequan.jeepay.request.PayOrderCreateRequest;
import com.jeequan.jeepay.response.JeepayResponse;
import com.jeequan.jeepay.response.PayOrderCreateResponse;
import org.apache.commons.lang3.StringUtils;

/**
 * @author chenjiawei
 * @desc
 * @Date 2023/08/28 00:08
 **/
public class PlspayKit {

    public static PayOrderCreateResponse payRequest(PayOrderDO payOrder,
                                                    MchAppConfigContext mchAppConfigContext,
                                                    PayOrderCreateReqModel model,Long ifId) throws JeepayException {

        // 发起统一下单
        PayOrderCreateResponse response = new PayOrderCreateResponse();
        ConfigContextQueryService configContextQueryService = SpringUtil.getBean(ConfigContextQueryService.class);
        PlspayNormalMchParams normalMchParams = (PlspayNormalMchParams)
                configContextQueryService.queryNormalMchParams(mchAppConfigContext.getMchId(),
                        mchAppConfigContext.getAppId(), ifId);
        // 构建请求数据
        PayOrderCreateRequest request = new PayOrderCreateRequest();
        // 商户号
        model.setMchNo(normalMchParams.getMerchantNo());
        // 应用ID
        model.setAppId(normalMchParams.getAppId());
        // 商户订单号
        model.setMchOrderNo(String.valueOf(payOrder.getId()));
        // 金额，单位分
        model.setAmount(payOrder.getAmount());
        // 币种，目前只支持cny
        model.setCurrency(payOrder.getCurrency());
        // 发起支付请求客户端的IP地址
        model.setClientIp(payOrder.getClientIp());
        // 商品标题
        model.setSubject(payOrder.getSubject());
        // 商品描述
        model.setBody(payOrder.getBody());
        request.setBizModel(model);

        if (normalMchParams.getSignType().equals(PlspayConfig.DEFAULT_SIGN_TYPE) || StringUtils.isEmpty(normalMchParams.getSignType())) {
            JeepayClient jeepayClient = JeepayClient.getInstance(normalMchParams.getAppId(),
                    normalMchParams.getAppSecret(), Jeepay.getApiBase());
            response = jeepayClient.execute(request);

        } else if (normalMchParams.getSignType().equals(PlspayConfig.SIGN_TYPE_RSA2)) {
            JeepayClient jeepayClient = JeepayClient.getInstance(normalMchParams.getAppId(),
                    normalMchParams.getRsa2AppPrivateKey(), Jeepay.getApiBase());
            response = jeepayClient.executeByRSA2(request);
        }
        return response;
    }

    public static Boolean checkPayResp(JeepayResponse response ,
                                       MchAppConfigContext mchAppConfigContext,Long ifId) {
        ConfigContextQueryService configContextQueryService = SpringBeansUtil.getBean(ConfigContextQueryService.class);
        PlspayNormalMchParams normalMchParams = (PlspayNormalMchParams) configContextQueryService
                .queryNormalMchParams(mchAppConfigContext.getMchId(),
                        mchAppConfigContext.getAppId(), ifId);

        boolean isSuccess = false;
        if (normalMchParams.getSignType().equals(PlspayConfig.DEFAULT_SIGN_TYPE) || StringUtils.isEmpty(normalMchParams.getSignType())) {
            isSuccess = response.isSuccess(normalMchParams.getAppSecret());

        } else if (normalMchParams.getSignType().equals(PlspayConfig.SIGN_TYPE_RSA2)) {
            isSuccess = response.isSuccessByRsa2(normalMchParams.getRsa2PayPublicKey());
        }

        return isSuccess;
    }
}
